package com.example.airhockeytextured.objects

import android.opengl.GLES20
import com.example.airhockeytextured.Constants
import com.example.airhockeytextured.data.VertexArray
import com.example.airhockeytextured.program.TextureShaderProgram

/**
 * @author Liujianyou
 * @date 2020/8/6
 * @des
 */
class Table {
    /**
     * 位置分量计数
     * */
    private val POSITION_COMPOMENT_COUNT = 2

    /**
     * 纹理坐标分量计数
     * */
    private val TEXTURE_COORDINATES_COMPOMENT_COUNT = 2

    private val vertexArray: VertexArray

    /**
     * 跨距
     * */
    private val STRIDE =
        (POSITION_COMPOMENT_COUNT + TEXTURE_COORDINATES_COMPOMENT_COUNT) * Constants.BYTES_PER_FLOAT

    /**
     * 这个数组包含了空气曲棍球桌子的顶点数据。我们定义了x和y的位置，以及s和t纹理坐标
     * t分量需要按照y分量相反的方向定义
     * */
    private val VERTEX_DATA = floatArrayOf(
        //Order of coordinates:X,Y,S,T
        //Triangle Fan
        0f, 0f, 0.5f, 0.5f,
        -0.5f, -0.8f, 0f, 0.9f,
        0.5f, -0.8f, 1f, 0.9f,
        0.5f, 0.8f, 1f, 0.1f,
        -0.5f, 0.8f, 0f, 0.1f,
        -0.5f, -0.8f, 0f, 0.9f
    )

    init {
        vertexArray = VertexArray(vertexData = VERTEX_DATA)
    }


    fun bindData(program: TextureShaderProgram) {
        vertexArray.setVertexAttributePointer(
            0,
            program.getPositionAttributeLocation(),
            POSITION_COMPOMENT_COUNT,
            STRIDE
        )
        vertexArray.setVertexAttributePointer(
            POSITION_COMPOMENT_COUNT,
            program.getTextureCoordinatesAttributeLocation(),
            TEXTURE_COORDINATES_COMPOMENT_COUNT,
            STRIDE
        )
    }

    fun draw() {

        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6)
    }
}